// Written by Dale J Williams.
// Licensed under MPL 1.1 unless otherwise specified. 
// http://www.mozilla.org/MPL/MPL-1.1.html

package engineDJW.display {
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BitmapDataChannel;
	import flash.geom.Point;
	
	/**
	 * ...
	 * @author Dale J Williams
	 */
	public class AlphaBitmap extends Bitmap {
		
		private var _alphaBMD	:BitmapData;
		private var _diffuseBMD	:BitmapData;
		private var _combined	:BitmapData;
		
		public function AlphaBitmap(diffuseBitmapOrData:Object, alphaBitmapOrData:Object) {
			_diffuseBMD = getBitmapFromObject(diffuseBitmapOrData);
			_alphaBMD = getBitmapFromObject(alphaBitmapOrData);
			init();
		}
		
		private function init():void {
			_combined = new BitmapData(_diffuseBMD.width, _diffuseBMD.height);
			_combined.copyPixels(_diffuseBMD, _diffuseBMD.rect, new Point());
			_combined.copyChannel(_alphaBMD, _alphaBMD.rect, new Point(), BitmapDataChannel.RED, BitmapDataChannel.ALPHA);
			this.bitmapData = _combined;
		}
		
		private function getBitmapFromObject(bitmapOrData:Object):BitmapData {
			if (bitmapOrData is BitmapData) 
				return bitmapOrData as BitmapData;
			if (bitmapOrData is Bitmap)
				return Bitmap(bitmapOrData).bitmapData;
			throw new Error("Invalid Bitmap Data");
		}
		
	}

}